{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def KDJCalculation(K1, D1, high, low, close):\n",
    "    #设定KDJ基期值\n",
    "    #count = 9\n",
    "    #设定k、d平滑因子a、b，不过目前已经约定俗成，固定为1/3\n",
    "    a = 1.0/3\n",
    "    b = 1.0/3\n",
    "    # 取得过去count天的最低价格\n",
    "    low_price = low #low.min() #min(list1)\n",
    "    # 取得过去count天的最高价格\n",
    "    high_price = high #high.max() #max(list1)\n",
    "    # 取得当日收盘价格\n",
    "    current_close = close\n",
    "    if high_price!=low_price:\n",
    "        #计算未成熟随机值RSV(n)＝（Ct－Ln）/（Hn-Ln）×100\n",
    "        RSV = (current_close-low_price)/(high_price-low_price)*100\n",
    "    else:\n",
    "        RSV = 50\n",
    "    #当日K值=(1-a)×前一日K值+a×当日RSV\n",
    "    K2=(1-a)*K1+a*RSV\n",
    "    #当日D值=(1-a)×前一日D值+a×当日K值\n",
    "    D2=(1-b)*D1+b*K2\n",
    "    #计算J值\n",
    "    J2 = 3*K2-2*D2\n",
    "    #log.info(\"Daily K1: %s, D1: %s, K2: %s, D2: %s, J2: %s\" % (K1,D1,K2,D2,J2))\n",
    "    return K1,D1,K2,D2,J2\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
